package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryGradeScopeListReq;
import com.sg.dto.base.framework.req.QueryGradeScopeDetailReq;
import com.sg.dto.base.framework.req.QueryGradeScopeListReq;
import com.sg.dto.base.framework.req.QueryGradeScopeValueListReq;
import com.sg.entity.GradeScope;
import com.sg.service.base.framework.MGradeScopeService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.GradeScopeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MGradeScopeServiceImpl
        extends ServiceImpl<GradeScopeMapper, GradeScope>
        implements MGradeScopeService {

    @Autowired
    GradeScopeMapper gradeScopeMapper;

    /**
     * 1-3-01批量删除等级范围[1802]
     * gen by moon at 10/16/2022, 11:07:03 AM
     */
    @Trace(operationName = "1-3-01批量删除等级范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteGradeScope(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-3-02批量新增等级范围[1801]
     * gen by moon at 10/16/2022, 11:07:02 AM
     */
    @Trace(operationName = "1-3-02批量新增等级范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddGradeScope(List<GradeScope> gradeScope) {

        if (gradeScope == null || gradeScope.size() == 0) {
            return false;
        }

        return super.saveBatch(gradeScope.stream().map(item -> {
            item.setGradeScopeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setGradeScopeCode("GS");//CUSTOM_CONVENTION
            item.setBatchEndTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
            item.setSubjectLifeCycle("EDITING");
            item.setIsArchive("FALSE");
            item.setArchivngType("NOT_ARCHIVE");
            item.setIsEditing("TRUE");
            item.setIsUpdate("TRUE");
            item.setUpdateStatus("NEW");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 1-3-01批量查询等级范围列表[1483]
     * gen by moon at 10/16/2022, 11:07:02 AM
     */
    @Trace(operationName = "1-3-01批量查询等级范围列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GradeScope> batchQueryGradeScopeList(BatchQueryGradeScopeListReq reqDto) {

        try {
            QueryWrapper<GradeScope> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getOrgGradeScopeList() == null || reqDto.getOrgGradeScopeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getOrgGradeScopeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(GradeScope.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(GradeScope.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(GradeScope.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<GradeScope>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-02批量修改等级范围[1296]
     * gen by moon at 10/16/2022, 11:07:01 AM
     */
    @Trace(operationName = "1-3-02批量修改等级范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateGradeScope(List<GradeScope> gradeScope) {

        if (gradeScope == null || gradeScope.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(gradeScope);

    }

    /**
     * 1-3-01查询等级范围列表[127]
     * gen by moon at 10/16/2022, 11:07:00 AM
     */
    @Trace(operationName = "1-3-01查询等级范围列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GradeScope> queryGradeScopeList(QueryGradeScopeListReq reqDto) {

        try {
            QueryWrapper<GradeScope> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(GradeScope.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(GradeScope.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(GradeScope.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<GradeScope>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-01查询等级范围详情[126]
     * gen by moon at 10/16/2022, 11:07:00 AM
     */
    @Trace(operationName = "1-3-01查询等级范围详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public GradeScope queryGradeScopeDetail(QueryGradeScopeDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<GradeScope> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<GradeScope> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                GradeScope retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-01查询等级范围值列表[125]
     * gen by moon at 10/16/2022, 11:06:59 AM
     */
    @Trace(operationName = "1-3-01查询等级范围值列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GradeScope> queryGradeScopeValueList(QueryGradeScopeValueListReq reqDto) {

        try {
            QueryWrapper<GradeScope> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(GradeScope.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(GradeScope.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(GradeScope.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<GradeScope>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-01删除等级范围[124]
     * gen by moon at 10/16/2022, 11:06:58 AM
     */
    @Trace(operationName = "1-3-01删除等级范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteGradeScope(String gradeScopeId) {

        return super.removeById(gradeScopeId);
    }

    /**
     * 1-3-01修改等级范围[123]
     * gen by moon at 10/16/2022, 11:06:58 AM
     */
    @Trace(operationName = "1-3-01修改等级范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateGradeScope(GradeScope gradeScope) {
        gradeScope.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        gradeScope.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(gradeScope);
    }

    /**
     * 1-3-01新增等级范围[122]
     * gen by moon at 10/16/2022, 11:06:57 AM
     */
    @Trace(operationName = "1-3-01新增等级范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addGradeScope(GradeScope gradeScope) {
        gradeScope.setBatchStartTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        gradeScope.setBatchEndTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        gradeScope.setSubjectLifeCycle("EDITING");
        gradeScope.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        gradeScope.setIsValid("FALSE");
        gradeScope.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        gradeScope.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(gradeScope)) {
            return gradeScope.getGradeScopeId();
        } else {
            return "-1";
        }

    }

    /**
     * M1查询等级范围测试列表[4918]
     * gen by moon at 10/16/2022, 11:07:04 AM
     */
    @Trace(operationName = "M1查询等级范围测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GradeScope> queryGradeScopeTestDataList(GradeScope gradeScope) {

        try {
            QueryWrapper<GradeScope> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(gradeScope);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(GradeScope.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(GradeScope.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(GradeScope.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<GradeScope>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删等级范围测试[4919]
     * gen by moon at 10/16/2022, 11:07:04 AM
     */
    @Trace(operationName = "M1批量删等级范围测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteGradeScopeTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
